package cn.swust.designpatternlearningplatform.controller;


import cn.swust.designpatternlearningplatform.adapter.TokenInfoBoAdapter;
import cn.swust.designpatternlearningplatform.common.AnnotationUtils.NotNull;
import cn.swust.designpatternlearningplatform.common.exception.CustomException;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.common.utils.JwtUtils;
import cn.swust.designpatternlearningplatform.common.utils.PasswordManger;

import cn.swust.designpatternlearningplatform.domain.dto.TestPaperDto;
import cn.swust.designpatternlearningplatform.domain.entity.Student;
import cn.swust.designpatternlearningplatform.domain.entity.Teacher;
import cn.swust.designpatternlearningplatform.domain.vo.LoginVo;
import cn.swust.designpatternlearningplatform.service.StudentService;
import cn.swust.designpatternlearningplatform.service.TeacherService;

import cn.swust.designpatternlearningplatform.service.TeacherTestService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/public")
public class PublicController {
    @Resource
    TeacherService teacherService;
    @Resource
    TeacherTestService teacherTestService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private static final ObjectMapper mapper = new ObjectMapper();
    /**
     * @param loginVo:
     * @return R<Object>
     * @author wang
     * @description 根据账号密码登录并将身份属性保存在token里面
     * @date 2023/4/16 12:01
     */
    @PostMapping("/login")
    @NotNull
    public R<Object> login(@RequestBody LoginVo loginVo){
        //提交的代码先md5加密
        String password = loginVo.getPassword();
        String number = loginVo.getNumber();
        if (password == null) {
            throw new CustomException("密码不能为空");
        }
        password = PasswordManger.pwdDigest(password);
        //根据客户端提交的账号比对

        Student st = studentService.getStudentByNumber(number);
        log.info(String.valueOf(st));
        if (st == null) {

            Teacher te = teacherService.getTeacherByNumber(number);
            if (te == null) {
                return R.error("登录失败，请检查工号和密码！");
            }
            if (!te.getTeacherPassword().equals(password)) {
                return R.error("登录失败，密码错误！");
            }

            String token = JwtUtils.getToken(TokenInfoBoAdapter.toTokenInfo(te));
            return R.success(te, "teacher", token);
        }
        if (!st.getStudentPassword().equals(password)) {
            return R.error("登录失败，密码错误！");
        }

        String token = JwtUtils.getToken(TokenInfoBoAdapter.toTokenInfo(st));
        log.info(token);
        try {
            String json = mapper.writeValueAsString(st);
            stringRedisTemplate.opsForValue().set(String.valueOf(st.getStudentNumber()), json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return R.success(st, "student", token);
    }




}
